package com.ynding.cloud.redis.test;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p> </p>
 *
 * @author ynding
 * @version 2022/1/4
 **/
@Service
@CacheConfig(cacheNames = "my-redis-cache1", cacheManager = "redisCacheManager")
public class AnimalServiceImpl {

    @Cacheable(key = "#id",sync = true)
    public Object getAnimal(Integer id) {
        System.out.println("操作数据库，返回Animal");
        return new Object();
    }


    /**
     * 使用@CachePut注解的方法，一定要有返回值，该注解声明的方法缓存的是方法的返回结果。
     * it always causes the
     * method to be invoked and its result to be stored in the associated cache
     **/
    @CachePut(key = "#animal.getId()")
    public Object setAnimal(Object animal) {
        System.out.println("存入数据库");
        return animal;
    }

    @CacheEvict(key = "#id")
    public void deleteAnimal(Integer id) {
        System.out.println("删除数据库中animal");
    }

    @CachePut(key = "#animal.getId()")
    public Object updateAnimal(Object animal) {
        System.out.println("修改animal,并存入数据库");
        return animal;
    }
}
